package com.oauth.facebook.server;

import java.util.List;

import org.apache.log4j.Logger;
import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.components.client.rpc.exception.SessionExpiredException;
import com.oauth.facebook.client.beans.FacebookModel;
import com.oauth.facebook.client.beans.FacebookUser;
import com.oauth.facebook.client.exception.FacebookOAuthException;
import com.oauth.facebook.client.rpc.FacebookRemoteService;
import com.oauth.facebook.server.service.FacebookService;
import com.rapwaves.feeds.Feeds;

public class FacebookGwtController extends GWTSpringController implements FacebookRemoteService {

	private Logger logger = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = -5758464341967618224L;

	@Autowired
	private FacebookService facebookService;

	public List<FacebookUser> getConnections() throws FacebookOAuthException {
		try {
			Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
			List<FacebookUser> connections = facebookService.getFriends(memberId);
			return connections;
		} catch (Exception e) {
			logger.error("Error in fetching scraps", e);
			throw new FacebookOAuthException();
		}
	}

	public FacebookModel getHomeData() throws FacebookOAuthException {
		FacebookModel model = new FacebookModel();
		model.setLoginView(false);
		logger.info("Entering facebook home controller");

		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		FacebookUser profile;
		try {
			profile = facebookService.getFacebookUser(memberId);
		} catch (Exception e) {
			logger.error("Error while fetching facebook user",e);
			throw new FacebookOAuthException();
		}
		model.setUser(profile);
		return model;
	}

	public List<FacebookUser> getUpdates() throws SessionExpiredException, FacebookOAuthException {
		logger.info("Getting FB updates...");
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		logger.info("MemberId:" + memberId);
		try {
			return facebookService.getUpdates(memberId);
		} catch (Exception e) {
			logger.error("Exception while getting facebook updates", e);
			throw new FacebookOAuthException(e);
		}
	}

	@Override
	public boolean updateStatus(String pText) throws SessionExpiredException,
			FacebookOAuthException {
		try {
			Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
			facebookService.updateStatus(pText, memberId);
			return true;
		} catch (Exception e) {
			logger.error("Error in fetching scraps", e);
			throw new FacebookOAuthException();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.oauth.facebook.client.rpc.FacebookRemoteService#getFeeds()
	 */
	@Override
	public Feeds getFeeds() throws SessionExpiredException, FacebookOAuthException {
		logger.info("Getting FB updates...");
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		logger.info("MemberId:" + memberId);
		try {
			return facebookService.getFeeds(memberId);
		} catch (Exception e) {
			logger.error("Exception while getting facebook updates", e);
			throw new FacebookOAuthException(e);
		}
	}

}
